import api from '../../api'
import app from './app'
import axios from "axios"

const cart = {
  state: {
    category: [],
    allGoods: [],
    searchGoods: []
  },

  getters: {
    getCategory: state => state.category,
    getGoods: state => state.allGoods,
    getSearchGoods: state => state.searchGoods
  },

  mutations: {
    setCategory (state, data) {
      state.category = data
    },
    setGoods (state, data) {
      state.allGoods = data
    },
    setSearchGoods (state, data) {
      state.searchGoods = data
    }
  },

  actions: {
    fetchCategory ({ commit }, data) {
      commit('setCategory', data)
    },
    fetchGoods ({ commit }, data) {
      axios.post(api.good.search, data)
        .then(res=>{
          app.state.isloading = false
          commit('setGoods', res.data)
        })
    },
    fetchSearchGoods ({ commit }, data) {
      axios.post(api.good.search, data)
        .then(res=>{
          app.state.isloading = false
          commit('setSearchGoods', res.data.data)
        })
    }
    
  }
}

export default cart
